﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Luxury_car_sales_system
{
    public partial class Customer_maintain : Form
    {
        public Customer_maintain()
        {
            InitializeComponent();
        }
        private void Customer_maintain_Load(object sender, EventArgs e)
        {
            string sql = "select * from Customer";
            DBHelper.dataSet.Tables.Clear();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = DBHelper.getDataTable(sql);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sex = "";
            if (radioButton1.Checked == true)
            {
                sex = radioButton1.Text;
            }
            else if (radioButton2.Checked == true)
            {
                sex = radioButton2.Text;
            }
            string sql = string.Format("update Customer set CustomerName='{0}',Sex='{1}',Phone='{2}',Workaddress='{3}',Address='{4}' where CustomerID='{5}'",textBox2.Text, sex, textBox3.Text, textBox5.Text, textBox4.Text,textBox1.Text);
            bool a = DBHelper.ExcuteNonQuery(sql);
            if (a == true)
            {
                MessageBox.Show("修改成功！");
                string sql1 = "select * from Customer";
                DBHelper.dataSet.Tables.Clear();
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = DBHelper.getDataTable(sql1);
            }
            else if (a == false)
            {
                MessageBox.Show("修改失败");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string index = dataGridView1.SelectedCells[0].Value.ToString(); 
            string sql = string.Format("delete from Customer where CustomerID={0}",index);
            bool b = DBHelper.ExcuteNonQuery(sql);
            if (b == true)
            {
                MessageBox.Show("删除成功");
                string sql1 = "select * from Customer";
                DBHelper.dataSet.Tables.Clear();
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = DBHelper.getDataTable(sql1);
            }
            else if (b == false)
            {
                MessageBox.Show("删除失败");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
